package be.kuleuven.peno3.mobiletoledo.view;

import java.util.Calendar;

import android.app.Activity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import be.kuleuven.peno3.mobiletoledo.R;
import be.kuleuven.peno3.mobiletoledo.Data.Client.MenuClient;

public class AlmaView extends Activity {
	private LinearLayout ll;
	@Override
	public void onCreate(Bundle b) {
		super.onCreate(b);
		
		Calendar cal = Calendar.getInstance();
		String[] menu = MenuClient.getMenu("alma3", cal);
		if(menu == null){
			Toast.makeText(this, "Er is geen internetverbinding", Toast.LENGTH_SHORT).show();
			finish();
		}
		else if(menu[0].equals("none") & menu.length == 1) {
			Toast.makeText(this, "Het menu van vandaag is niet beschikbaar", Toast.LENGTH_SHORT).show();
			finish();
		}
		else {
		String[] dishes = new String[4];
		dishes[0] = "Menu Alma van " +cal.get(Calendar.DATE)+"/"+(cal.get(Calendar.MONTH)+1)+"/"+cal.get(Calendar.YEAR);
		dishes[1] = "Voorgerecht: \n" + stringFilter(menu[0]);
		dishes[2] = "Hoofdgerecht: \n" + stringFilter(menu[1]);
		dishes[3] = "Nagerecht: \n" +stringFilter(menu[2]);

		displayDishes(dishes);
		}
	}
	
	private String stringFilter(String string){
		while(string.contains(" \n")){
			string = string.replaceAll(" \n", "\n");
		}
		String[] split = string.split(" ");
		String filter = split[0];
		if (string.contains("\n")){
			filter = "-" + filter;
		}
		for (int i = 1; i < split.length; i++){
			if (split[i].length() != 0){
				filter = filter + " " + split[i].trim();
			}
		}
		filter = filter.replaceAll("\n", "\n-");
		return filter;
	}
	
	private void displayDishes(String[] dishes){
		setContentView(R.layout.textlistlayout);
		
		TextView manQuestBar = (TextView) findViewById(R.id.top_bar);
		manQuestBar.setText("Alma");
		ll = (LinearLayout) findViewById(R.id.man_quest_ll);
		
		for (int i = 0; i < dishes.length; i++){
			ImageView image = new ImageView(this.getApplicationContext());
			if(i > 1){
				image.setImageDrawable(getResources().getDrawable(R.drawable.line));
				ll.addView(image);
			}
			else if(i == 1){
				image.setImageDrawable(getResources().getDrawable(R.drawable.line_full));
				ll.addView(image);
			}
			
			TextView text = new TextView(this.getApplicationContext());
			text.setText(dishes[i]);
			if(i==0){
				text.setTextSize(TypedValue.COMPLEX_UNIT_PT, 10);
				text.setTextColor(0xFF69931e);
				text.setGravity(Gravity.CENTER_HORIZONTAL);
			}
			else{
				text.setTextSize(TypedValue.COMPLEX_UNIT_PT, 8);
				text.setPadding(0, 7, 0, 7);
			}
			ll.addView(text);
		}
	}
}
